/*
 * Button.cpp
 */

#include "Button.h"

namespace gui
  {
    Button::Button()
      {
        _pressed = false;
      }

    void Button::init()
      {
        bgGc().mode(DRAW_MODE_SOLID);
        fgGc().mode(DRAW_MODE_SOLID);
        borderGc().mode(DRAW_MODE_SOLID);
      }

    void Button::process()
      {
        _pressed = (hasFocus() && mouse_b);
      }

    void Button::paint(BITMAP *buffer, int x_offset, int y_offset)
      {
        int x1, y1, x2, y2;
        int color1, color2;
        int delay;

        if (!_pressed)
          {
            color1 = 0xaaaaaa;
            color2 = 0x222222;
            delay = 0;
          }
        else
          {
            color1 = 0x222222;
            color2 = 0xaaaaaa;
            delay = 1;
          }

        bgGc().activate();

        x1 = x_offset + x();
        y1 = y_offset + y();
        x2 = x_offset + x() + width();
        y2 = y_offset + y() + height();
        rectfill(buffer, x1, y1, x2, y2, makecol(0,21,82));

        borderGc().activate();

        x1 = x_offset + x();
        y1 = y_offset + y();
        x2 = x_offset + x() + width() -1;
        y2 = y_offset + y() + height() -1;
        rect(buffer, x1, y1, x2, y2, color1);

        x1++;
        y1++;
        x2++;
        y2++;
        rect(buffer, x1, y1, x2, y2, color2);

        fgGc().activate();

        x1 = x_offset + x() + (width() * 0.5) + delay;
        y1 = y_offset + y() + (height() * 0.5) - 4 + delay;
        textout_centre_ex(buffer, font, title().c_str(), x1, y1, 0xffffff, -1);
      }

    std::string const& Button::title() const { return _title; }

    std::string const& Button::title(std::string title) 
      { 
        return _title = title; 
      }

    GraphicContext& Button::bgGc() { return _bgGc; }
    GraphicContext& Button::fgGc() { return _fgGc; }
    GraphicContext& Button::borderGc() { return _borderGc; }
  }

